import {reactive} from "vue";
import {deepClone} from "../utils";

/**
 * 生成一个可以重置的reactive
 * @param data
 */
export function useResetReactive<T extends object>(data: T): [T, () => void] {
    const state = reactive(deepClone(data)) as T;
    const reset = () => {
        Object.keys(data).forEach((key) => delete state[key as keyof T]);
        Object.assign(state, deepClone(data));
    };
    return [state, reset] as const;
}
